package com.ruoyi.job.service;

import com.bytedance.ads.ApiClient;
import com.bytedance.ads.ApiException;
import com.bytedance.ads.model.ToolsLogSearchV2Response;
import com.bytedance.ads.model.ToolsLogSearchV2ResponseDataLogsInner;
import com.ruoyi.bill.service.IBillPortService;
import com.ruoyi.common.utils.redis.RedisUtils;
import com.ruoyi.ocean.domain.bo.OceanAccountBo;
import com.ruoyi.ocean.domain.bo.OceanLogBo;
import com.ruoyi.ocean.domain.bo.OceanLogTotalBo;
import com.ruoyi.ocean.domain.vo.OceanAccountVo;
import com.ruoyi.ocean.domain.vo.OceanLogTotalVo;
import com.ruoyi.ocean.mapper.OceanLogTotalMapper;
import com.ruoyi.ocean.service.IOceanAccountService;
import com.ruoyi.ocean.service.IOceanLogService;
import com.ruoyi.ocean.service.OpenApiService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * XxlJob开发示例（Bean模式）
 * <p>
 * 开发步骤：
 * 1、任务开发：在Spring Bean实例中，开发Job方法；
 * 2、注解配置：为Job方法添加注解 "@XxlJob(value="自定义jobhandler名称", init = "JobHandler初始化方法", destroy = "JobHandler销毁方法")"，注解value值对应的是调度中心新建任务的JobHandler属性的值。
 * 3、执行日志：需要通过 "XxlJobHelper.log" 打印执行日志；
 * 4、任务结果：默认任务结果为 "成功" 状态，不需要主动设置；如有诉求，比如设置任务结果为失败，可以通过 "XxlJobHelper.handleFail/handleSuccess" 自主设置任务结果；
 *
 * @author xuxueli 2019-12-11 21:52:51
 */
@Slf4j
@Service
public class BillJobService {
    private final IBillPortService iBillPortService;
    private final OpenApiService openApiService;
    private final IOceanLogService iOceanLogServic;
    private final IOceanAccountService iOceanAccountService;

    private final OceanLogTotalMapper baseMapper;
    @Autowired
    private MongoTemplate mongoTemplate;
    public BillJobService(IBillPortService iBillPortService, OpenApiService openApiService, IOceanLogService iOceanLogServic, IOceanAccountService iOceanAccountService, OceanLogTotalMapper baseMapper) {
        this.iBillPortService = iBillPortService;
        this.openApiService = openApiService;
        this.iOceanLogServic = iOceanLogServic;
        this.iOceanAccountService = iOceanAccountService;
        this.baseMapper = baseMapper;
    }

    /**
     * 匹配所有需要转户的客户，端口台账
     */
    @XxlJob("BillJobHandler")
    public void BillJobHandler() throws Exception {
        iBillPortService.transfer();
        // default success
    }

    /**
     * 自动获取前一天数据*
     */
    @XxlJob("OceanLogJobHandler")
    public void oceanLogJobHandler() throws Exception {
       openApiService.oceanLog();
    }

    /**
     * 可以每天获取多次
     */
    @XxlJob("OceanLogTotaJobHandler")
    public void oceanLogTotaJobHandler() throws Exception {
        mongoTemplate.dropCollection(OceanLogTotalVo.class);
        List<OceanLogTotalVo> list = baseMapper.selectTotalNoPage();

        List<OceanLogTotalVo> list2 = baseMapper.selectTotal(new OceanLogTotalBo());
        List<OceanLogTotalVo> org = list;
        org.forEach(item -> {
            for (OceanLogTotalVo item1 : list2) {
                if(item.getAccountId().equals(item1.getAccountId()) && item.getMonth().equals(item1.getMonth())){
                    item.setDTotal(item1.getDTotal());
                    item.setQTotal(item.getTotal() - item1.getDTotal());

                }
            }
        });
        long ids = new Date().getTime();
        for (OceanLogTotalVo item : org) {
            ids++;
            if(item.getDTotal()==null){
                item.setDTotal(0L);
                item.setQTotal(item.getTotal() - item.getDTotal());
            }
            item.setId(ids);
        }




        mongoTemplate.insertAll(org);
    }
    /**
     * 操作日志自动刷新token*
     * @throws Exception
     */
    @XxlJob("RefTokenJobHandler")
    public void RefTokenJobHandler() throws Exception {
        openApiService.refToken();
        // default success
    }

    @XxlJob("AccessTokenJobHandler")
    public void AccessTokenJobHandler() throws Exception {
        openApiService.getAccessToken();
        // default success
    }


//    每日一次
    @XxlJob("AddAccountJobHandler")
    public void AddAccountJobHandler() throws Exception {
        List<Map<String, Object>> list= openApiService.getParams();
        list.forEach(item->{
            try {
                openApiService.addAccount(item);
            } catch (ApiException e) {
                throw new RuntimeException(e);
            }
        });
        // default success
    }


    //    每日一次
    @XxlJob("ReportAgent")
    public void ReportAgentJobHandler() throws Exception {

      openApiService.reportAgent();
        // default success
    }

}
